package net.hekka.canyoutalk.web.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.hekka.canyoutalk.data.constant.ErrorCode;
import net.hekka.canyoutalk.data.vo.list.TempLogQueryVoList;
import net.hekka.canyoutalk.share.property.RequestParameterProperties;
import net.hekka.canyoutalk.web.processor.TempLogQueryProcessor;
import net.hekka.canyoutalk.web.vo.LogAggregationVoList;
import net.hekka.canyoutalk.web.vo.PageVo;

/**
 * TempLogQuery용 Business Layer를 담당하는 Servlet Class
 * @author jeffreyha
 */
public class TempLogQueryServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        System.setProperty("file.encoding", "utf-8");
        request.setCharacterEncoding("utf-8");

        //파라메터 받아오는 requestParameterProperties
        RequestParameterProperties requestParameterProperties = getRequestParameterProperties(request);

        String serviceId = requestParameterProperties.getProperty("serviceId");
        System.out.println("serviceId : [" + serviceId + "]");

        System.out.println("requestParameterProperties : [" + requestParameterProperties.toString() + "]");

        if ("getStatusChangeList".equals(serviceId)) {
            System.out.println("# getStatusChangeList is called.");
            int errorCode = getStatusChangeList(request, requestParameterProperties);
            if (errorCode == ErrorCode.SUCCESS) {
                System.out.println("[[getStatusChangeList]] requestParameterProperties [" + requestParameterProperties.toString() + "]");
                request.getRequestDispatcher("./statusChangeList.jsp").forward(request, response);
            } else if (errorCode == ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST) {
                System.out.println("[getStatusChangeList ERR] statusChangeList.jsp call...[" + errorCode + "]");
                request.getRequestDispatcher("./statusChangeList.jsp").forward(request, response);
            } else {
                System.out.println("[getStatusChangeList ERR] statusChangeList.jsp call...[" + errorCode + "]");
                response.sendRedirect("system/canyoutalk_error001.jsp");  // Go to Fail Page.
            }
        } else if ("getBroadcastList".equals(serviceId)) {
            System.out.println("# getBroadcastList is called.");
            int errorCode = getBroadcastList(request, requestParameterProperties);
            if (errorCode == ErrorCode.SUCCESS) {
                System.out.println("[[getBroadcastList]] requestParameterProperties [" + requestParameterProperties.toString() + "]");
                request.getRequestDispatcher("./broadcastCheck.jsp").forward(request, response);
            } else if (errorCode == ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST) {
                System.out.println("[getBroadcastList ERR] broadcastCheck.jsp call...[" + errorCode + "]");
                request.getRequestDispatcher("./broadcastCheck.jsp").forward(request, response);
            } else {
                System.out.println("[getBroadcastList ERR] broadcastCheck.jsp call...[" + errorCode + "]");
                response.sendRedirect("system/canyoutalk_error001.jsp");  // Go to Fail Page.
            }
        } else if ("getNormalView".equals(serviceId)) {
            System.out.println("# getNormalView is called.");
            int errorCode = getNormalViewList(request, requestParameterProperties);
            if (errorCode == ErrorCode.SUCCESS) {
                System.out.println("[[getNormalViewList]] requestParameterProperties [" + requestParameterProperties.toString() + "]");
                request.getRequestDispatcher("./normalView.jsp").forward(request, response);
            } else if (errorCode == ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST) {
                System.out.println("[getNormalViewList ERR] normalView.jsp call...[" + errorCode + "]");
                request.getRequestDispatcher("./normalView.jsp").forward(request, response);
            } else {
                System.out.println("[getNormalViewList ERR] normalView.jsp call...[" + errorCode + "]");
                response.sendRedirect("system/canyoutalk_error001.jsp");  // Go to Fail Page.
            }
        } else if ("getDetailView".equals(serviceId)) {
            System.out.println("# getDetailView is called.");
            int errorCode = getDetailViewList(request, requestParameterProperties);
            if (errorCode == ErrorCode.SUCCESS) {
                System.out.println("[[getDetailViewList]] requestParameterProperties [" + requestParameterProperties.toString() + "]");
                request.getRequestDispatcher("./detailView.jsp").forward(request, response);
            } else if (errorCode == ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST) {
                System.out.println("[getDetailViewList ERR] detailView.jsp call...[" + errorCode + "]");
                request.getRequestDispatcher("./detailView.jsp").forward(request, response);
            } else {
                System.out.println("[getDetailViewList ERR] detailView.jsp call...[" + errorCode + "]");
                response.sendRedirect("system/canyoutalk_error001.jsp");  // Go to Fail Page.
            }
        } else if ("getDetailAggregation".equals(serviceId)) {
            System.out.println("# getDetailAggregation is called.");
            int errorCode = getDetailAggregation(request, requestParameterProperties);
            if (errorCode == ErrorCode.SUCCESS) {
                System.out.println("[[getDetailAggregation]] requestParameterProperties [" + requestParameterProperties.toString() + "]");
                request.getRequestDispatcher("./detailAggregation.jsp").forward(request, response);
            } else if (errorCode == ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST) {
                System.out.println("[getDetailAggregation ERR] detailAggregation.jsp call...[" + errorCode + "]");
                request.getRequestDispatcher("./detailAggregation.jsp").forward(request, response);
            } else {
                System.out.println("[getDetailAggregation ERR] detailAggregation.jsp call...[" + errorCode + "]");
                response.sendRedirect("system/canyoutalk_error001.jsp");  // Go to Fail Page.
            }
        } else if ("getMyFriendAggregation".equals(serviceId)) {
            System.out.println("# getMyFriendAggregation is called.");
            int errorCode = getMyFriendAggregation(request, requestParameterProperties);
            if (errorCode == ErrorCode.SUCCESS) {
                System.out.println("[[getMyFriendAggregation]] requestParameterProperties [" + requestParameterProperties.toString() + "]");
                request.getRequestDispatcher("./myFriendAggregation.jsp").forward(request, response);
            } else if (errorCode == ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST) {
                System.out.println("[getMyFriendAggregation ERR] myFriendAggregation.jsp call...[" + errorCode + "]");
                request.getRequestDispatcher("./myFriendAggregation.jsp").forward(request, response);
            } else {
                System.out.println("[getMyFriendAggregation ERR] myFriendAggregation.jsp call...[" + errorCode + "]");
                response.sendRedirect("system/canyoutalk_error001.jsp");  // Go to Fail Page.
            }
        }

    }

    private int getBroadcastList(HttpServletRequest httpServletRequest, RequestParameterProperties requestParameterProperties) {
        int errorCode = ErrorCode.SUCCESS;
        TempLogQueryProcessor tempLogQueryProcessor = new TempLogQueryProcessor();

        TempLogQueryVoList tempLogQueryVoList = tempLogQueryProcessor.getBroadcastList(requestParameterProperties);
        TempLogQueryVoList friendStateList = tempLogQueryProcessor.getFriendStateList(requestParameterProperties);

//        if (tempLogQueryVoList == null) {
//            errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NULL;
//        } else {
            httpServletRequest.setAttribute("tempLogQueryVoList", tempLogQueryVoList);
            httpServletRequest.setAttribute("friendStateList", friendStateList);
            httpServletRequest.setAttribute("sequence01", requestParameterProperties.getProperty("sequence01"));
//        }
        return errorCode;
    }

    private int getStatusChangeList(HttpServletRequest httpServletRequest, RequestParameterProperties requestParameterProperties) {
        int errorCode = ErrorCode.SUCCESS;
        TempLogQueryProcessor tempLogQueryProcessor = new TempLogQueryProcessor();
        boolean isExist = requestParameterProperties.containsKey("totalCount");
        if (isExist == false) {
            int totalCount = tempLogQueryProcessor.getTotalCountOfStatusChange();
            System.out.println("totalCount [" + totalCount + "]");
            requestParameterProperties.put("totalCount", Integer.toString(totalCount));
        }

        Hashtable hashtable = tempLogQueryProcessor.getStatusChangeList(requestParameterProperties);
        TempLogQueryVoList tempLogQueryVoList = (TempLogQueryVoList) hashtable.get("tempLogQueryVoList");
        PageVo pageVo = (PageVo) hashtable.get("pageVo");
        if (tempLogQueryVoList == null) {
            errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NULL;
        } else {
            if (tempLogQueryVoList.size() == 0) {
                errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST;
                httpServletRequest.setAttribute("totalCount", "0");
            } else {
                httpServletRequest.setAttribute("tempLogQueryVoList", tempLogQueryVoList);
                httpServletRequest.setAttribute("pageVo", pageVo);
                httpServletRequest.setAttribute("queryCount", requestParameterProperties.getProperty("queryCount"));
                httpServletRequest.setAttribute("nowPage", requestParameterProperties.getProperty("nowPage"));
                httpServletRequest.setAttribute("totalCount", requestParameterProperties.getProperty("totalCount"));
            }
        }
        return errorCode;
    }

    private int getNormalViewList(HttpServletRequest httpServletRequest, RequestParameterProperties requestParameterProperties) {
        int errorCode = ErrorCode.SUCCESS;
        TempLogQueryProcessor tempLogQueryProcessor = new TempLogQueryProcessor();
        boolean isExist = requestParameterProperties.containsKey("totalCount");
        if (isExist == false) {
            int totalCount = tempLogQueryProcessor.getTotalCountOfNormalView();
            requestParameterProperties.put("totalCount", Integer.toString(totalCount));
        }

        Hashtable hashtable = tempLogQueryProcessor.getNormalView(requestParameterProperties);
        TempLogQueryVoList tempLogQueryVoList = (TempLogQueryVoList) hashtable.get("tempLogQueryVoList");
        PageVo pageVo = (PageVo) hashtable.get("pageVo");
        if (tempLogQueryVoList == null) {
            errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NULL;
        } else {
            if (tempLogQueryVoList.size() == 0) {
                errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST;
                httpServletRequest.setAttribute("totalCount", "0");
            } else {
                httpServletRequest.setAttribute("tempLogQueryVoList", tempLogQueryVoList);
                httpServletRequest.setAttribute("pageVo", pageVo);
                httpServletRequest.setAttribute("queryCount", requestParameterProperties.getProperty("queryCount"));
                httpServletRequest.setAttribute("nowPage", requestParameterProperties.getProperty("nowPage"));
                httpServletRequest.setAttribute("totalCount", requestParameterProperties.getProperty("totalCount"));
            }
        }
        return errorCode;
    }

    private int getDetailViewList(HttpServletRequest httpServletRequest, RequestParameterProperties requestParameterProperties) {
        int errorCode = ErrorCode.SUCCESS;
        TempLogQueryProcessor tempLogQueryProcessor = new TempLogQueryProcessor();
        boolean isExist = requestParameterProperties.containsKey("totalCount");
        if (isExist == false) {
            int totalCount = tempLogQueryProcessor.getTotalCountOfDetailView();
            System.out.println("totalCount [" + totalCount + "]");
            requestParameterProperties.put("totalCount", Integer.toString(totalCount));
        }

        Hashtable hashtable = tempLogQueryProcessor.getDetailView(requestParameterProperties);
        TempLogQueryVoList tempLogQueryVoList = (TempLogQueryVoList) hashtable.get("tempLogQueryVoList");
        PageVo pageVo = (PageVo) hashtable.get("pageVo");
        if (tempLogQueryVoList == null) {
            errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NULL;
        } else {
            if (tempLogQueryVoList.size() == 0) {
                errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST;
                httpServletRequest.setAttribute("totalCount", "0");
            } else {
                httpServletRequest.setAttribute("tempLogQueryVoList", tempLogQueryVoList);
                httpServletRequest.setAttribute("pageVo", pageVo);
                httpServletRequest.setAttribute("queryCount", requestParameterProperties.getProperty("queryCount"));
                httpServletRequest.setAttribute("nowPage", requestParameterProperties.getProperty("nowPage"));
                httpServletRequest.setAttribute("totalCount", requestParameterProperties.getProperty("totalCount"));
            }
        }
        return errorCode;
    }

    private int getDetailAggregation(HttpServletRequest httpServletRequest, RequestParameterProperties requestParameterProperties) {
        int errorCode = ErrorCode.SUCCESS;
        TempLogQueryProcessor tempLogQueryProcessor = new TempLogQueryProcessor();
        boolean isExist = requestParameterProperties.containsKey("totalCount");
        if (isExist == false) {
            int totalCount = tempLogQueryProcessor.getTotalCountOfDetailView();
            System.out.println("totalCount [" + totalCount + "]");
            requestParameterProperties.put("totalCount", Integer.toString(totalCount));
        }

        Hashtable hashtable = tempLogQueryProcessor.getDetailLogAggregationList(requestParameterProperties);
        LogAggregationVoList logAggregationVoList = (LogAggregationVoList) hashtable.get("logAggregationVoList");
        PageVo pageVo = (PageVo) hashtable.get("pageVo");
        if (logAggregationVoList == null) {
            errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NULL;
        } else {
            if (logAggregationVoList.size() == 0) {
                errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST;
                httpServletRequest.setAttribute("totalCount", "0");
            } else {
                httpServletRequest.setAttribute("logAggregationVoList", logAggregationVoList);
                httpServletRequest.setAttribute("pageVo", pageVo);
                httpServletRequest.setAttribute("queryCount", requestParameterProperties.getProperty("queryCount"));
                httpServletRequest.setAttribute("nowPage", requestParameterProperties.getProperty("nowPage"));
                httpServletRequest.setAttribute("totalCount", requestParameterProperties.getProperty("totalCount"));
            }
        }

        System.out.println("### requestParameterProperties : " + requestParameterProperties.toString());
        return errorCode;
    }

    private int getMyFriendAggregation(HttpServletRequest httpServletRequest, RequestParameterProperties requestParameterProperties) {
        int errorCode = ErrorCode.SUCCESS;
        TempLogQueryProcessor tempLogQueryProcessor = new TempLogQueryProcessor();
        boolean isExist = requestParameterProperties.containsKey("totalCount");
        if (isExist == false) {
            int totalCount = tempLogQueryProcessor.getTotalCountOfStatusChange();
            System.out.println("totalCount [" + totalCount + "]");
            requestParameterProperties.put("totalCount", Integer.toString(totalCount));
        }

        Hashtable hashtable = tempLogQueryProcessor.getMyFriendAggregationList(requestParameterProperties);
        LogAggregationVoList logAggregationVoList = (LogAggregationVoList) hashtable.get("logAggregationVoList");
        PageVo pageVo = (PageVo) hashtable.get("pageVo");
        if (logAggregationVoList == null) {
            errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NULL;
        } else {
            if (logAggregationVoList.size() == 0) {
                errorCode = ErrorCode.TEMPLOGQUERYSERVLET_LIST_IS_NOT_EXIST;
                httpServletRequest.setAttribute("totalCount", "0");
            } else {
                httpServletRequest.setAttribute("logAggregationVoList", logAggregationVoList);
                httpServletRequest.setAttribute("pageVo", pageVo);
                httpServletRequest.setAttribute("queryCount", requestParameterProperties.getProperty("queryCount"));
                httpServletRequest.setAttribute("nowPage", requestParameterProperties.getProperty("nowPage"));
                httpServletRequest.setAttribute("totalCount", requestParameterProperties.getProperty("totalCount"));
            }
        }

        System.out.println("### requestParameterProperties : " + requestParameterProperties.toString());
        return errorCode;
    }

    
    /**
     * Requst객체의 Parameters의 값을 저장한 Properties객체를 구한다.
     * @param httpServletRequest Request객체
     * @return Requst객체의 Parameters의 값을 저장한 Properties객체
     */
    private RequestParameterProperties getRequestParameterProperties(HttpServletRequest httpServletRequest) {
        RequestParameterProperties requestParameterPropeties = new RequestParameterProperties();
        Enumeration params = httpServletRequest.getParameterNames();
        while (params.hasMoreElements()) {
            String paramName = (String) params.nextElement();
            String paramValue = httpServletRequest.getParameter(paramName);
            requestParameterPropeties.put(paramName, paramValue);
        }
        return requestParameterPropeties;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
